export const validateNumberFixed = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^\d+(\.\d{2})$/;

	if (value != '' && !reg.test(value)) {
		callback(new Error(`${rule.label}必须是数字(2位小数)`));
	} else {
		callback();
	}
};

export const validateNumberFixed2 = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^\d+(\.\d{2})$/;
	if (!reg.test(value) && rule.required) {
		callback(new Error(`${rule.label}必须是数字(2位小数)`));
	} else {
		callback();
	}
};

//不超过min~max位数字()
export const validateNumberFixed4 = (min: number, max: number) => (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^[0-9]*$/;
	//reg.test(value) ==true满足条件
	if (reg.test(value) && value.toString().length >= min && value.toString().length <= max) {
		callback();
	} else {
		callback(new Error(`${rule.label}必须是(${min}~${max}位数字)`));
	}
};
//验证手机号
export const validateNumberPhone = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^([1][3,4,5,6,7,8,9])\d{9}$/;
	//reg.test(value) ==true满足条件
	if (reg.test(value) || value == '') {
		callback();
	} else {
		callback(new Error(`${rule.label}必须是手机号码`));
	}
};
//验证邮箱
export const validateEmail = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/;

	if (value != '' && !reg.test(value)) {
		callback(new Error(`请输入正确的邮箱`));
	} else {
		callback();
	}
};

//0-100
export const validate_one_to_hundred = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^([0-9]{1,2}$)|(^[0-9]{1,2}\.[0-9]{1,2}$)|100$/;

	if (value != '' && !reg.test(value)) {
		callback(new Error(`${rule.label}必须在0-100之间`));
	} else {
		callback();
	}
};

//校验是数字（可带小数）
export const validateNumber = (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg = /^\d+(\.\d+)?$/;
	if (value != '' && !reg.test(value)) {
		callback(new Error(`${rule.label}必须为数字`));
	} else {
		callback();
	}
};

// 截取百分比数字
export const validatePercentageNumber = (param: any) => {
	let reg = /\d+(\.\d+)?%/;
	// const a = reg.test(param);
	const result = param.match(reg, '')[0];
	return result;
};

//不能超过x位小数
export const validateNumberLimit = (x: number) => (rule: any, value: any, callback: Function) => {
	if (value == '' && rule.required) {
		callback(new Error(`${rule.label}不能为空`));
	}
	let reg: any;
	switch (x) {
		case 0:
			reg = /^\d+(\.\d{0,0})?$/;
			break;
		case 1:
			reg = /^\d+(\.\d{0,1})?$/;
			break;
		case 2:
			reg = /^\d+(\.\d{0,2})?$/;
			break;
		case 3:
			reg = /^\d+(\.\d{0,3})?$/;
			break;
		case 4:
			reg = /^\d+(\.\d{0,4})?$/;
			break;
		case 5:
			reg = /^\d+(\.\d{0,5})?$/;
			break;
		case 6:
			reg = /^\d+(\.\d{0,6})?$/;
			break;
		case 7:
			reg = /^\d+(\.\d{0,7})?$/;
			break;
		case 8:
			reg = /^\d+(\.\d{0,8})?$/;
			break;
		case 9:
			reg = /^\d+(\.\d{0,9})?$/;
			break;
	}

	//reg.test(value) ==true满足条件
	if (reg.test(value) && value != '') {
		callback();
	} else {
		callback(new Error(`${rule.label}不能超过(${x}位小数)`));
	}
};
